package com.company.card_service.api;


import com.company.card_service.config.loginInterceptConfig.JwtHelper;
import com.company.card_service.model.UserInfo;
import com.company.card_service.model.UserLoginLog;
import com.company.card_service.serviceImpl.UserInfoServiceImpl;
import com.company.card_service.serviceImpl.UserLoginLogServiceImpl;
import com.company.card_service.vo.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/company/userInfo")
public class LoginController {


    @Autowired
    private UserInfoServiceImpl userInfoService;

    @Autowired
    private UserLoginLogServiceImpl userLoginLogService;

    @PostMapping(value = {"AuthLogin/{employeeId}","AuthLogin"})
    @ApiImplicitParam(dataType = "Long",name = "employeeId",value = "员工id",required = false)
    @ApiOperation("小程序登录认证")
    public Result AuthLogin(HttpServletRequest request,
                            @RequestBody UserInfo userInfo,
                            @PathVariable(required = false, value = "") Long employeeId){
        Map map;
        if(employeeId != null){
            map = userInfoService.AuthLogin(employeeId,request,userInfo);
        }
        else {
            map = userInfoService.AuthLogin(request,userInfo);
        }
        return Result.success(map);
    }
    @GetMapping("logout/{userId}")
    @ApiOperation("小程序退出登录")
    public Result logout(@PathVariable Integer userId){
        UserLoginLog byId = userLoginLogService.getById(userId);
        byId.setLogoutTime(new Date());
        byId.setUpdateTime(new Date());
        boolean b = userLoginLogService.updateById(byId);
        return Result.success(b);
    }
}
